package com.xzmzhp.dao.people.SQL;

import com.xzmzhp.pojo.people.Society;
import com.xzmzhp.utils.StringUtils;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author 王源
 * @Date: 2020/11/15 21:25
 * @Description: 社团动态sql
 */
public class SocietySQL {

    public String findBySocial(String societyName, String societySchool) {
        return new SQL() {
            {
                SELECT("society_id,society_name,society_school");
                FROM("society");
                if (StringUtils.isNotEmpty(societyName)) {
                    WHERE("society_name COLLATE utf8_general_ci like '%" + societyName + "%'");
                }
                if (StringUtils.isNotEmpty(societySchool)) {
                    WHERE("society_school COLLATE utf8_general_ci like '%" + societySchool + "%'");
                }
            }
        }.toString();
    }

    /**
     * 社团信息修改sql语句
     *
     * @param society 社团实体类
     * @return sql语句
     */
    public String updateSocial(Society society) {
        return new SQL() {
            {
                //所需修改的表名
                UPDATE("society");
                //所需要的列名
                if (society != null) {
                    //如果社团名称不等于空就修改社团名称信息
                    if (society.getSocietyName() != null && !"".equals(society.getSocietyName())) {
                        SET("society_name = '" + society.getSocietyName() + "'");
                        //判断id是否正确
                        if (society.getSocietyId() > 0) {
                            WHERE("society_id = " + society.getSocietyId());
                        }
                    }
                    //如果学校名称不等于空就修改学校名称信息
                    if (society.getSocietySchool() != null && !"".equals(society.getSocietySchool())) {
                        SET("society_school = '" + society.getSocietySchool() + "'");
                        //判断id是否正确
                        if (society.getSocietyId() > 0) {
                            WHERE("society_id = " + society.getSocietyId());
                        }
                    }
                }
            }

        }.toString();
    }
}
